package com.coscoshipping.rams.demo.service.impl;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.coscoshipping.rams.demo.entity.BlockAnalysis;
import com.coscoshipping.rams.demo.entity.BlockHeader;
import com.coscoshipping.rams.demo.mapper.BlockHeaderMapper;
import com.coscoshipping.rams.demo.pojo.dto.*;
import com.coscoshipping.rams.demo.service.IBlockHeaderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import org.springframework.stereotype.Service;
import com.coscoshipping.rams.boot.handler.customException.BusinessException;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zhaoyin.yang
 * @since 2025-01-15
 */
@Service
public class BlockHeaderServiceImpl extends ServiceImpl<BlockHeaderMapper, BlockHeader> implements IBlockHeaderService {


    /**
     * @description 根据blockname查询block
     * @param blockname
     * @return: java.util.List<com.coscoshipping.rams.demo.entity.BlockAnalysis>
     * @author 杨召银
     * @date 2025/2/8 8:51
     */
    @Override
    @DS("master")
    public List<BlockAnalysis> getBlockHeaderList(String blockname) {
        return Convert.toList(BlockAnalysis.class,
                this.lambdaQuery()
                    .eq(BlockHeader::getBlocksta,"O")
                    .like(StrUtil.isNotBlank(blockname),BlockHeader::getBlockname,blockname)
                    .orderByDesc(BlockHeader::getEffectbegin)
                    .list()
        );
    }



    /**
     * @description 获取活动开始当天的预控总数
     * @return: java.util.List<com.coscoshipping.rams.demo.pojo.dto.BlockSnapshotDTO>
     * @author 杨召银
     * @date 2025/1/15 17:50
     */
    @Override
    @DS("master")
    public List<BlockSnapshotDTO> getTodayBlockSnapshot(){
        return this.baseMapper.getTodayBlockSnapshot();
    }



    /**
     * @description 团队主单关联Block
     * @param groupToBlockDTO
     * @return: void
     * @author 杨召银
     * @date 2025/1/16 11:55
     */
    @Transactional(rollbackFor = Exception.class)
    @DS("master")
    @Override
    public void connectGroupToBlock(GroupToBlockDTO groupToBlockDTO){
        // 根据团队主单，更新blockcode
        this.baseMapper.connectMasterToBlock(groupToBlockDTO);
        this.baseMapper.connectHmasterToBlock(groupToBlockDTO);
    }



    /**
     * @description 查询指定block的团队列表
     * @param blockcode
     * @return: java.util.List<com.coscoshipping.rams.demo.pojo.dto.GroupInfoDTO>
     * @author 杨召银
     * @date 2025/2/8 9:19
     */
    @DS("master")
    @Override
    public List<GroupInfoDTO> getGroupByBlockcode(String blockcode){
        return this.baseMapper.getGroupByBlockcode(blockcode);
    }



    /**
     * @description 获取指定团队主单的收入信息、支付信息"
     * @param groupno
     * @return: com.coscoshipping.rams.demo.pojo.dto.GroupInfoDTO
     * @author 杨召银
     * @date 2025/2/8 11:46
     */
    @DS("master")
    @Override
    public GroupInfoDTO getGroupInfo(String groupno){
        // 获取团队信息
        GroupInfoDTO groupInfoDTO=this.baseMapper.getGroupByCode(groupno);
        if(ObjectUtil.isNull(groupInfoDTO)){
            throw new BusinessException("团队主单不存在");
        }

        // 获取指定团队消费信息
        GroupIncomeDTO groupIncomeDTO=this.baseMapper.groupIncome(groupno);
        groupInfoDTO.setGroupIncomeDTO(groupIncomeDTO);

        // 获取指定团队支付信息
        List<GroupPayDTO> groupPayDTO=this.baseMapper.groupPay(groupno);
        groupInfoDTO.setGroupPayDTO(groupPayDTO);

        return groupInfoDTO;
    }


    /**
     * @description 获取指定block的收入信息、支付信息
     * @param blockcode
     * @return: java.util.List<com.coscoshipping.rams.demo.pojo.dto.GroupInfoDTO>
     * @author 杨召银
     * @date 2025/2/10 14:48
     */
    @DS("master")
    @Override
    public List<GroupInfoDTO> getBlockInfo(String blockcode){
        List<GroupInfoDTO> infoDTOList=new ArrayList<>();
        List<GroupInfoDTO>  groupInfoDTOList=this.baseMapper.getGroupByBlockcode(blockcode);
        for(GroupInfoDTO infoDTO: groupInfoDTOList){
            infoDTOList.add(getGroupInfo(infoDTO.getAccnt()));
        }
        return infoDTOList;
    }
}
